﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Configuration;

namespace BOMS.WebUI.Web.UC
{
    public partial class ChartOutput : System.Web.UI.UserControl
    {
        IEnumerable _DataSource;
       public IEnumerable DataSource
        {
            set { _DataSource = value; }
            get { return _DataSource; }
        }
        string xField;
        public string XField
        {
            set { xField = value; }
            get { return xField; }
        }
        string yFields;
         public string YFields
                {
                  set { yFields = value; }
                    get { return yFields; }
        }
        string _OtherField;
        public string OtherField
        {
            set { _OtherField = value; }
            get { return _OtherField; }
        }
        SeriesChartType _ChartType= SeriesChartType.Line;
        public SeriesChartType ChartType
        {
            set { _ChartType = value; }
            get { return _ChartType; }
        }
        Color _ChartColor=Color.Red;
        public Color ChartColor
        {
            set { _ChartColor = value; }
            get { return _ChartColor; }
        }
        Boolean _Show3D;
        public Boolean Show3D
        {
            set { _Show3D = value; }
            get { return _Show3D; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }
       public void CharDataBind()
        {
            SetChart();
        }
        private void SetChart()
        {
            Series ser;
            string seriesName = "数据";
            switch(YFields)
            {
                case "Vval":
                    seriesName = "电压";
                   // ChartColor = Color.Red;
                   break;
                case "Ival":
                   seriesName = "电流";
                   // ChartColor = Color.Red;
                   break;
                case "Rval":
                    seriesName = "内阻";
                  //  ChartColor = Color.LightPink;
                    break;
                case "Tval":
                    seriesName = "温度";
                   // ChartColor = Color.Yellow;
                    break;
                case "CapFree":
                    seriesName = "剩余容量";
                    // ChartColor = Color.Red;
                    break;
            }
            ser = CreateSeries(seriesName, XField, YFields, ChartType, ChartColor);           
            MyChart.Series.Add(ser);

            if (XField.ToLower().IndexOf("time") > -1)
            {
                MyChart.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false;
                MyChart.ChartAreas[0].AxisX.LabelStyle.TruncatedLabels = true;
                MyChart.ChartAreas[0].AxisX.LabelStyle.Format = "{0:MM-dd HH:mm:ss}";//文本格式
            }
            MyChart.ChartAreas[0].Area3DStyle.Enable3D = Show3D;
            MyChart.ChartAreas[0].BorderWidth = 0; 
            MyChart.ChartAreas[0].AxisX.IsReversed = false;
            MyChart.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Lines;
            MyChart.ChartAreas[0].AxisY.IsReversed = false;
           
            MyChart.DataBind();
        }
        private Series CreateSeries(string SeriesName,string xfield ,string yfield,SeriesChartType chartType,Color color)
        {
            Series Vser = new Series(SeriesName);
            Vser.IsValueShownAsLabel = true;
            Vser.ChartType = chartType;// SeriesChartType.Line;//线条类型（Line-折线，SpLine-曲线）

           // Vser.MarkerStyle = MarkerStyle.Circle;//线条上的数据点标志类型      
           // Vser.MarkerSize = 6;//线条上的数据点大小
           // Vser.Color = color;//线条颜色
           
            if (Vser.ChartType != SeriesChartType.FastLine && Vser.ChartType != SeriesChartType.FastPoint)
            {
                Vser.BorderWidth = 1;
                Vser.ShadowOffset = 1;
                Vser.BackGradientStyle = GradientStyle.VerticalCenter;
            }
           
            Vser.Points.DataBind(DataSource, xfield, yfield, OtherField);
            
            return Vser;
        }
    }
}